## Created by EmpowerStats @ Fri, 15 Nov 24 18:33:27 +0800## 
#******************** Regarding ALL Following R Code   ******************** 
#*** COPYRIGHT (c) 2010, 2021 X&Y Solutions, ALL RIGHT RESERVED *********** 
#******************* www.EmpowerStats.com ********************************* 
#************************************************************************** 
Sys.setlocale(category = 'LC_ALL', locale = 'English_United States.1252'); 
.libPaths(file.path(R.home(),'library')); 
library(doBy); 
options(timeout=600); 
library(plotrix); 
library(stringi); 
library(stringr); 
library(survival); 
library(rms); 
library(nnet); 
library(car); 
library(mgcv); 
pdfwd<-6; pdfht<-6; 
load('/Users/liukangning/Desktop/EmpowerROS/Analysis/cognitivedepression/Body/BMX_H.Rdata'); 
if (length(which(ls()=='EmpowerStatsR'))==0) EmpowerStatsR<-get(ls()[1]); 
names(EmpowerStatsR)<-toupper(names(EmpowerStatsR)); 
originalVNAME<-names(EmpowerStatsR); 
ofname<-'Body_1_tbl'; 
vname<-c(NA)[-1]; 
vlabel<-c(NA)[-1]; 
varused4this <- c(''); 
pkgs<-c(''); 
for (g in pkgs) {  
if (!(g %in% rownames(installed.packages()))) install.packages(g,repos='https://cloud.r-project.org'); 
}
library(); 
WD <- EmpowerStatsR; rm(EmpowerStatsR); gc(); 
title<-'输出数据文件'; 
wd.subset=''; 
svname<-c(); 
xvname<-c(); 
par2<- NA; 
par3<- NA; 
chk<- 0; 
dec<-4; 
##R package##   ##R package##;
mat2htmltable<-function(mat) {
  t1<- apply(mat,1,function(z) paste(z,collapse="</td><td>"))
  t2<- paste("<tr><td>",t1,"</td></tr>")
  t3<- paste(t2,collapse="");
  return(t2)
}
ofile <- paste(ofname,"xls",sep=".")
if (chk == 1) {
  WDTMP<-t(rbind(colnames(WD),WD)); rm(WD); gc(); 
  write.table(WDTMP,file=ofile,sep='\t',row.names=FALSE,col.names=FALSE,quote=FALSE)
  o.vname <- paste(WDTMP[1,], collapse=", ")
} else {
  if (length(xvname)==0) xvname<-colnames(WD)
  if (length(svname)>0) {
    vx <- match(svname,xvname)
    vx <- vx[!is.na(vx)]
    if (length(vx)>0) xvname<-xvname[-vx]
  }
  if (length(svname)>0) xvname<-c(svname,xvname)
  WDTMP<-cbind(WD[,xvname])
  tmp.n<-nrow(WDTMP)
  if (length(svname)>0) {
    n.sv<-length(svname)
    for (i in (n.sv:1)) {
      WDTMP<- WDTMP[order(WDTMP[,i], decreasing=(sdf[i]>0)),]
    }
    outputLvl <- max(n.sv-1, 1)
    if (outputLvl>1) {
      idx <- apply(WDTMP[,1:outputLvl],1,function(z) paste(z,collapse="_"))
    } else {
      idx <- WDTMP[,1]
    }
    output.obs1 <- unlist(tapply(rep(1,tmp.n),factor(idx),function(z) cumsum(z)-1))
    output.obs2 <- unlist(tapply(rep(1,tmp.n),factor(idx),function(z) sum(z)-cumsum(z)))
  } else {
    output.obs1 <- (1:tmp.n)
    output.obs2 <- (tmp.n:1)
  }
  slt <- NULL
  if (is.numeric(par2)) slt <- (output.obs1 < par2)
  if (is.numeric(par3)) {
    slt2 <- (output.obs2 < par3)
    if (!is.null(slt)) {slt <- slt | slt2;} else {slt <- slt2;}
  }
  if (!is.null(slt)) WDTMP <- WDTMP[slt,]
  write.table(WDTMP,file=ofile,sep='\t',row.names=FALSE,col.names=TRUE,quote=FALSE)
  WDTMP<-rbind(colnames(WDTMP),as.matrix(WDTMP))
  o.vname <- paste(colnames(WDTMP), collapse=", ")
}
o.nobs <- nrow(WDTMP)-1
o.nvar <- ncol(WDTMP)
tmp.note<-""
if (o.nobs>100) {tmp.note<-paste(tmp.note,"</br>More records ..."); WDTMP<-cbind(WDTMP[1:100,])}
if (o.nvar>20) {tmp.note<-paste(tmp.note,"</br> More variables ...");WDTMP<-cbind(WDTMP[,1:20])}

w<-c("<!DOCTYPE html><html lang='zh'><head><meta charset='utf-8'></head><body>")
w<-c(w,paste("</br>Output file: ", ofile));
w<-c(w,paste("</br>Number of observations: ", o.nobs));
w<-c(w,paste("</br>Number of variables: ", o.nvar));
w<-c(w,"</br>Variables: ", o.vname);
w<-c(w,"</br></br>Data view<table border=3>",mat2htmltable(WDTMP),"</table>")
w<-c(w,tmp.note)
w<-c(w,wd.subset)
w<-c(w,paste("</br></br>Created by EmpowerStats (www.empowerstats.com) and R on",Sys.Date()))
w<-c(w,"</body></html>")
fileConn<-file(paste(ofname,".htm",sep=""));writeLines(w, fileConn)